package cn.tedu.ivos.dictoption.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.dict.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dictoption.mapper.DictOptionMapper;
import cn.tedu.ivos.dictoption.pojo.dto.DictOptionParam;
import cn.tedu.ivos.dictoption.pojo.dto.DictOptionQuery;
import cn.tedu.ivos.dictoption.pojo.entity.DictOption;
import cn.tedu.ivos.dictoption.pojo.vo.DictOptionVO;
import cn.tedu.ivos.dictoption.service.DictOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictOptionServiceImpl implements DictOptionService {


    @Autowired
    DictOptionMapper dictOptionMapper;
    @Autowired
    DictMapper dictMapper;

    @Override
    public List<DictOptionVO> selectlist(DictOptionQuery dictOptionQuery) {
        log.debug("dictOptionQuery:{}",dictOptionQuery);
       List<DictOptionVO> list=dictOptionMapper.selectlist(dictOptionQuery);
        return list;
    }


    @Override
    public void optionsave(DictOptionParam dictOptionParam) {
        log.debug("dictOptionParam:{}",dictOptionParam);
        DictOption dictOption=new DictOption();
        BeanUtils.copyProperties(dictOptionParam,dictOption);
        if(dictOption.getId()==null){
        dictOption.setCreateTime(new Date());
        dictOptionMapper.save(dictOption);
        }else{
            dictOption.setUpdateTime(new Date());
        dictOptionMapper.update(dictOption);
        }
    }

    @Override
    public void deleteDictOption(Long id) {
        log.debug("删除字典项业务参数:{}", id);
        dictOptionMapper.deleteById(id);
    }
    @Override
    public List<DictOptionVO> selectDictOptionByCode(String code) {
        log.debug("根据字典编码查询字典项业务参数:{}", code);
        //需要先根据条件中的字典code将字典VO查出来
        DictQuery dictQuery=new DictQuery();
        dictQuery.setCode(code);
        List<DictVO>  list= dictMapper.selectdict(dictQuery);
        if(list!=null  && list.size()>0){
            DictVO dictVO=list.get(0);
            DictOptionQuery  dictOptionQuery=new DictOptionQuery();
            dictOptionQuery.setDictId(dictVO.getId());
            List<DictOptionVO> selectlist = dictOptionMapper.selectlist(dictOptionQuery);
                 return selectlist;
        }else{
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
    }

}
